package com.ymatou.iapi.kafka.mqpublish.testcase;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.mqpublish.parameter.PublishBean;
import com.ymatou.iapi.kafka.mqpublish.service.PublishCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.database.mongo.MQMessageBean;
import com.ymttest.database.mongo.MQMessageCompensateBean;
import com.ymttest.database.mongo.MQMessageCompensateWapper;
import com.ymttest.database.mongo.MQMessageStatusBean;
import com.ymttest.database.mongo.MQMessageStatusWapper;
import com.ymttest.database.mongo.MQMessageWapper;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.constant.ECode;

import oracle.jdbc.oracore.StreamInfo;

/**
 * 发布消息用例 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_Publish {
	private static PublishBean publishBean;
	private static PublishCall publishCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("发布消息用例");
	}

	@Before
	public void caseUp() {
		publishBean = new PublishBean();
		publishCall = new PublishCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(publishCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_Publish_001() {
		Logger.start(true, "发布消息-200发布成功-接收成功");
		try {

			// String msguniqueid = TestDataManager.getRandomUUID().toString();
			String msguniqueid = TestDataManager.getRandomNum(10);
			String body = TestDataManager.randomStr(20);
			String appName = "kafkaApp";
			String codeName = "qusi";
			// String codeName = "miaosi";

			List<MQMessageStatusBean> statusList = new MQMessageStatusWapper().getStatusByMid(appName, msguniqueid);
			int num1 = statusList.size();

			publishBean.setAppId(appName);
			publishBean.setCode(codeName);
			publishBean.setIp("127.0.0.1");
			publishBean.setMsgUniqueId(msguniqueid);
			publishBean.setBody(body);

			publishCall.setData(publishBean);
			publishCall.callService();

			Thread.sleep(1000);

			Logger.verifyEquals(ECode.SUCCESS, publishCall.getCode(), "验证Code");
			Logger.verifyEquals("NULL", publishCall.getMsg().toUpperCase(), "验证Msg");

			MQMessageBean messageBean = new MQMessageWapper().getMessageByMid(appName, codeName, msguniqueid);

			int flag = 0;

			while (messageBean == null || messageBean.getPstatus() != 1 && flag < 20) {
				Thread.sleep(200);
				flag++;
				messageBean = new MQMessageWapper().getMessageByMid(appName, codeName, msguniqueid);
			}

			Date nowTime = new Date();
			Date date = new Date(nowTime.getTime() - 8 * 60 * 60 * 1000);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH");

			// 验证Message表
			Logger.verifyEquals(publishBean.getAppId(), messageBean.getAid(), "验证数据库Aid");
			Logger.verifyEquals(publishBean.getCode(), messageBean.getCode(), "验证数据库Code");
			Logger.verifyEquals(publishBean.getIp(), messageBean.getIp(), "验证数据库Ip");
			Logger.verifyEquals("\"" + publishBean.getBody() + "\"", messageBean.getBody(), "验证数据库Body");
			Logger.verifyEquals(sdf.format(date), messageBean.getCtime().toString().subSequence(7, 20), "验证数据库Ctime");
			Logger.verifyEquals(true, messageBean.getBusIp().contains("130"), "验证数据库BusIp");

			Logger.verifyEquals(0, messageBean.getNstatus(), "验证数据库nstatus");
			Thread.sleep(1000);

			String pushTime = messageBean.getPushtime().toString();

			Logger.verifyEquals(sdf.format(date), pushTime.subSequence(7, 20), "验证数据库Pushtime");

			Logger.verifyEquals(1, messageBean.getPstatus(), "验证数据库Pushstatus:1-分发成功");

			// 验证Status表生成记录
			statusList = new MQMessageStatusWapper().getStatusByMid(appName, msguniqueid);
			int num2 = statusList.size();

			Logger.verifyEquals(1, num2 - num1, "验证数据库MQ_Message_Status新生成一条记录");

			MQMessageStatusBean status = statusList.get(num2 - 1);

			Logger.verifyEquals(messageBean.getUuid(), status.getUuid(), "验证数据库MQ_Message_Status字段uuid");
			Logger.verifyEquals(messageBean.getMid(), status.getMid(), "验证数据库MQ_Message_Status字段Mid");
			Logger.verifyEquals("kafkaApp_qusi_c1", status.getCid(), "验证数据库MQ_Message_Status字段Cid");
			Logger.verifyEquals("PushOk", status.getStatus(), "验证数据库MQ_Message_Status字段Status");
			Logger.verifyEquals("Dispatch", status.getSource(), "验证数据库MQ_Message_Status字段Source");
			Logger.verifyEquals(sdf.format(date), status.getCtime().toString().subSequence(7, 20),
					"验证数据库MQ_Message_Status字段Ctime");
			Logger.verifyEquals(true, status.getResult().contains("ok, kafkaApp_qusi_c1"),
					"验证数据库MQ_Message_Status字段Result");
			Logger.verifyEquals(true,
					status.getResult().contains("http://mockforpay.iapi.ymatou.com/api/messagebus/return/ok"),
					"验证数据库MQ_Message_Status字段Result");
			// Logger.verifyEquals(true, status.getR_ip().contains("134"),
			// "验证数据库MQ_Message_Status字段R_ip");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	@Ignore
	public void Tc_Publish_002() {
		Logger.start(true, "发布消息-201-业务异常");
		try {

			publishCall.setData(publishBean);
			publishCall.callService();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_Publish_003() {
		Logger.start(true, "发布消息-400-参数异常-invalid appId");
		try {

			String msguniqueid = TestDataManager.getRandomUUID().toString();
			String body = TestDataManager.randomStr(20);
			publishBean.setAppId("testAuto");
			publishBean.setCode("gaoxu");
			publishBean.setIp("127.0.0.1");
			publishBean.setMsgUniqueId(msguniqueid);
			publishBean.setBody(body);

			publishCall.setData(publishBean);
			publishCall.callService();

			Logger.verifyEquals("400", publishCall.getCode(), "验证Code");
			Logger.verifyEquals("appId:[testAuto],queueCode:[gaoxu] not exist.", publishCall.getMsg(), "验证Msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_Publish_004() {
		Logger.start(true, "发布消息-400-参数异常-invalid code");
		try {

			String msguniqueid = TestDataManager.getRandomUUID().toString();
			String body = TestDataManager.randomStr(20);
			publishBean.setAppId("kafkaApp");
			publishBean.setCode("xud");
			publishBean.setIp("127.0.0.1");
			publishBean.setMsgUniqueId(msguniqueid);
			publishBean.setBody(body);

			publishCall.setData(publishBean);
			publishCall.callService();

			Logger.verifyEquals("400", publishCall.getCode(), "验证Code");
			Logger.verifyEquals("参数异常|invalid code:xud", publishCall.getMsg(), "验证Msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_Publish_005() {
		Logger.start(true, "发布消息-400-参数异常-appId空");
		try {

			String msguniqueid = TestDataManager.getRandomUUID().toString();
			String body = TestDataManager.randomStr(20);
			// publishBean.setAppId("testAuto");
			publishBean.setCode("kafkaCode");
			publishBean.setIp("127.0.0.1");
			publishBean.setMsgUniqueId(msguniqueid);
			publishBean.setBody(body);

			publishCall.setData(publishBean);
			publishCall.callService();

			Logger.verifyEquals("400", publishCall.getCode(), "验证Code");
			Logger.verifyEquals("appId not empty", publishCall.getMsg(), "验证Msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_Publish_006() {
		Logger.start(true, "发布消息-400-参数异常-Code空");
		try {

			String msguniqueid = TestDataManager.getRandomUUID().toString();
			String body = TestDataManager.randomStr(20);
			publishBean.setAppId("kafkaApp");
			// publishBean.setCode("gaoxu");
			publishBean.setIp("127.0.0.1");
			publishBean.setMsgUniqueId(msguniqueid);
			publishBean.setBody(body);

			publishCall.setData(publishBean);
			publishCall.callService();

			Logger.verifyEquals("400", publishCall.getCode(), "验证Code");
			Logger.verifyEquals("code not empty", publishCall.getMsg(), "验证Msg");

			MQMessageBean messageBean = new MQMessageWapper().getMessageByMid("testAuto", "gaoxu", msguniqueid);
			Logger.verifyIsNull(messageBean, "验证数据库未生成相应数据");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_Publish_007() {
		Logger.start(true, "发布消息-400-参数异常-msgUniqueId空");
		try {

			String msguniqueid = TestDataManager.getRandomUUID().toString();
			String body = TestDataManager.randomStr(20);
			publishBean.setAppId("kafkaApp");
			publishBean.setCode("kafkaCode");
			publishBean.setIp("127.0.0.1");
			// publishBean.setMsgUniqueId(msguniqueid);
			publishBean.setBody(body);

			publishCall.setData(publishBean);
			publishCall.callService();

			Logger.verifyEquals("400", publishCall.getCode(), "验证Code");
			Logger.verifyEquals("messageId not empty", publishCall.getMsg(), "验证Msg");

			MQMessageBean messageBean = new MQMessageWapper().getMessageByMid("testAuto", "gaoxu", msguniqueid);
			Logger.verifyIsNull(messageBean, "验证数据库未生成相应数据");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_Publish_008() {
		Logger.start(true, "发布消息-400-参数异常-body空");
		try {

			String msguniqueid = TestDataManager.getRandomUUID().toString();
			String body = TestDataManager.randomStr(20);
			publishBean.setAppId("kafkaApp");
			publishBean.setCode("kafkaCode");
			publishBean.setIp("127.0.0.1");
			publishBean.setMsgUniqueId(msguniqueid);
			// publishBean.setBody(body);

			publishCall.setData(publishBean);
			publishCall.callService();

			Logger.verifyEquals("200", publishCall.getCode(), "验证Code");
			Logger.verifyEquals("null", publishCall.getMsg(), "验证Msg");

			MQMessageBean messageBean = new MQMessageWapper().getMessageByMid("testAuto", "gaoxu", msguniqueid);
			Logger.verifyIsNull(messageBean, "验证数据库未生成相应数据");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_Publish_009() {
		Logger.start(true, "发布消息-200发布成功-没配DispatchGroup,不分发,不进补单库");
		try {

			String appName = "kafkaApp1";
			String codeName = "kafkaCode";

			// String msguniqueid = TestDataManager.getRandomUUID().toString();
			String msguniqueid = TestDataManager.getRandomNum(10);
			String body = TestDataManager.randomStr(20);
			publishBean.setAppId(appName);
			publishBean.setCode(codeName);

			publishBean.setIp("127.0.0.1");
			publishBean.setMsgUniqueId(msguniqueid);
			publishBean.setBody(body);

			publishCall.setData(publishBean);
			publishCall.callService();

			Logger.verifyEquals(ECode.SUCCESS, publishCall.getCode(), "验证Code");
			Logger.verifyEquals("NULL", publishCall.getMsg().toUpperCase(), "验证Msg");

			MQMessageBean messageBean = new MQMessageWapper().getMessageByMid(appName, codeName, msguniqueid);

			Logger.verifyEquals("\"" + publishBean.getBody() + "\"", messageBean.getBody(), "验证数据库Body");
			Logger.verifyEquals(publishBean.getIp(), messageBean.getIp(), "验证数据库IP");
			Logger.verifyEquals(1000, messageBean.getPushstatus(), "验证数据库Pushstatus");

			Logger.verifyEquals(0, messageBean.getNstatus(), "验证数据库nstatus");
			Logger.verifyIsNull(messageBean.getPushtime(), "验证数据库Pushtime未生成");

			// 验证进入补单库
			MQMessageCompensateBean compensate = new MQMessageCompensateWapper().getCompensateByUuid(appName, codeName,
					messageBean.getUuid());
			Logger.verifyIsNull(compensate, "验证未进补单库");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_Publish_010() {
		Logger.start(true, "发布消息-200发布成功-接收失败");
		try {

			String appName = "kafkaApp";
			String codeName = "youqusi";

			// String msguniqueid = TestDataManager.getRandomUUID().toString();
			String msguniqueid = TestDataManager.getRandomNum(10);
			String body = TestDataManager.randomStr(20);
			publishBean.setAppId(appName);
			publishBean.setCode(codeName);
			publishBean.setIp("127.0.0.1");
			publishBean.setMsgUniqueId(msguniqueid);
			publishBean.setBody(body);

			List<MQMessageStatusBean> statusList = new MQMessageStatusWapper().getStatusByMid(appName, msguniqueid);
			int num1 = statusList.size();

			publishCall.setData(publishBean);
			publishCall.callService();

			Logger.verifyEquals(ECode.SUCCESS, publishCall.getCode(), "验证Code");
			Logger.verifyEquals("NULL", publishCall.getMsg().toUpperCase(), "验证Msg");

			MQMessageBean messageBean = new MQMessageWapper().getMessageByMid(appName, codeName, msguniqueid);

			int flag = 0;

			while (messageBean == null || messageBean.getPstatus() != 2 && flag < 20) {
				Thread.sleep(200);
				flag++;
				messageBean = new MQMessageWapper().getMessageByMid(appName, codeName, msguniqueid);
			}

			Date nowTime = new Date();
			Date date = new Date(nowTime.getTime() - 8 * 60 * 60 * 1000);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH");

			// 验证消息成功
			Logger.verifyEquals(publishBean.getAppId(), messageBean.getAid(), "验证数据库Aid");
			Logger.verifyEquals(publishBean.getCode(), messageBean.getCode(), "验证数据库Code");
			Logger.verifyEquals(publishBean.getIp(), messageBean.getIp(), "验证数据库Ip");
			Logger.verifyEquals("\"" + publishBean.getBody() + "\"", messageBean.getBody(), "验证数据库Body");
			Logger.verifyEquals(sdf.format(date), messageBean.getCtime().toString().subSequence(7, 20), "验证数据库Ctime");
			Logger.verifyEquals(true, messageBean.getBusIp().contains("130"), "验证数据库BusIp");
			Logger.verifyEquals(2, messageBean.getNstatus(), "验证数据库nstatus:2-分发进入补单");
			Logger.verifyEquals(sdf.format(date), messageBean.getPushtime().toString().subSequence(7, 20),
					"验证数据库Pushtime");
			Logger.verifyEquals(2, messageBean.getPstatus(), "验证数据库pstatus:2-补单中");

			// 验证进入补单库
			MQMessageCompensateBean compensate = new MQMessageCompensateWapper().getCompensateByUuid(appName, codeName,
					messageBean.getUuid());

			Logger.verifyEquals(messageBean.getAid(), compensate.getAppid(), "验证数据库MQ_Message_Compensate字段AppId");
			Logger.verifyEquals(messageBean.getCode(), compensate.getCode(), "验证数据库MQ_Message_Compensate字段Code");
			Logger.verifyEquals(messageBean.getMid(), compensate.getMid(), "验证数据库MQ_Message_Compensate字段mId");
			// Logger.verifyEquals(sdf.format(date),
			// compensate.getRtimeout().toString().subSequence(7, 20),
			// "验证数据库MQ_Message_Compensate字段Rtimeout");
			Logger.verifyEquals(0, compensate.getRetrycount(), "验证数据库MQ_Message_Compensate字段RetryCount");
			Logger.verifyEquals(2, compensate.getSource(), "验证数据库MQ_Message_Compensate字段Source补单来源:2-分发站");
			Logger.verifyEquals(0, compensate.getNstatus(), "验证数据库MQ_Message_Compensate字段nStatus");
			Logger.verifyEquals("kafkaApp_youqusi_c1", compensate.getCid(), "验证数据库MQ_Message_Compensate字段Cid");

			// 验证Status表生成记录
			statusList = new MQMessageStatusWapper().getStatusByMid(appName, msguniqueid);
			int num2 = statusList.size();

			Logger.verifyEquals(1, num2 - num1, "验证数据库MQ_Message_Status新生成一条记录");

			MQMessageStatusBean status = statusList.get(num2 - 1);

			SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd'T'HH");

			Logger.verifyEquals(messageBean.getUuid(), status.getUuid(), "验证数据库MQ_Message_Status字段uuid");
			Logger.verifyEquals(messageBean.getMid(), status.getMid(), "验证数据库MQ_Message_Status字段Mid");
			Logger.verifyEquals("kafkaApp_youqusi_c1", status.getCid(), "验证数据库MQ_Message_Status字段Cid");
			Logger.verifyEquals("PushFail", status.getStatus(), "验证数据库MQ_Message_Status字段Source");
			Logger.verifyEquals("Dispatch", status.getSource(), "验证数据库MQ_Message_Status字段uuid");
			Logger.verifyEquals(sdf2.format(date), status.getCtime().toString().subSequence(7, 20),
					"验证数据库MQ_Message_Status字段Ctime");
			Logger.verifyEquals(true, status.getResult().contains("fail, kafkaApp_youqusi_c1"),
					"验证数据库MQ_Message_Status字段Result");
			Logger.verifyEquals(true,
					status.getResult().contains("http://mockforpay.iapi.ymatou.com/api/messagebus/return/fail"),
					"验证数据库MQ_Message_Status字段Result");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	@Ignore
	public void Tc_Publish_011() {
		Logger.start(true, "发布消息-200发布成功-IsRetry=0不补发");
		try {

			String appName = "kafkaApp";
			String codeName = "bubufa";

			// String msguniqueid = TestDataManager.getRandomUUID().toString();
			String msguniqueid = TestDataManager.getRandomNum(10);
			String body = TestDataManager.randomStr(20);
			publishBean.setAppId(appName);
			publishBean.setCode(codeName);
			publishBean.setIp("127.0.0.1");
			publishBean.setMsgUniqueId(msguniqueid);
			publishBean.setBody(body);

			List<MQMessageStatusBean> statusList = new MQMessageStatusWapper().getStatusByMid(appName, msguniqueid);
			int num1 = statusList.size();

			publishCall.setData(publishBean);
			publishCall.callService();

			Logger.verifyEquals(ECode.SUCCESS, publishCall.getCode(), "验证Code");

			MQMessageBean messageBean = new MQMessageWapper().getMessageByMid(appName, codeName, msguniqueid);

			int flag = 0;

			while (messageBean == null || messageBean.getPstatus() != 2 && flag < 20) {
				Thread.sleep(200);
				flag++;
				messageBean = new MQMessageWapper().getMessageByMid(appName, codeName, msguniqueid);
			}

			Date nowTime = new Date();
			Date date = new Date(nowTime.getTime() - 8 * 60 * 60 * 1000);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH");

			// 验证消息成功
			Logger.verifyEquals(publishBean.getAppId(), messageBean.getAid(), "验证数据库Aid");
			Logger.verifyEquals(publishBean.getCode(), messageBean.getCode(), "验证数据库Code");
			Logger.verifyEquals(publishBean.getIp(), messageBean.getIp(), "验证数据库Ip");
			Logger.verifyEquals("\"" + publishBean.getBody() + "\"", messageBean.getBody(), "验证数据库Body");
			// Logger.verifyEquals(sdf.format(date),
			// messageBean.getCtime().toString().subSequence(7, 20),
			// "验证数据库Ctime");
			Logger.verifyEquals(true, messageBean.getBusIp().contains("130"), "验证数据库BusIp");
			Logger.verifyEquals(0, messageBean.getNstatus(), "验证数据库nstatus:0-进入RabbitMQ");
			Logger.verifyEquals(3, messageBean.getPstatus(), "验证数据库pstatus:3-分发失败");

			// 验证不进入补单库
			MQMessageCompensateBean compensate = new MQMessageCompensateWapper().getCompensateByUuid(appName, codeName,
					messageBean.getUuid());

			Logger.verifyIsNull(compensate, "验证数据库MQ_Message_Compensate表没有生成相应数据");

			// 验证Status表生成记录
			statusList = new MQMessageStatusWapper().getStatusByMid(appName, msguniqueid);
			int num2 = statusList.size();

			Logger.verifyEquals(1, num2, "验证数据库MQ_Message_Status未生成一条补单记录");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_Publish_012() {
		Logger.start(true, "发布消息-200发布成功-接收失败-秒补");
		try {

			String appName = "kafkaApp";
			String codeName = "miaosi";

			// String msguniqueid = TestDataManager.getRandomUUID().toString();
			String msguniqueid = TestDataManager.getRandomNum(10);
			String body = TestDataManager.randomStr(20);
			publishBean.setAppId(appName);
			publishBean.setCode(codeName);
			publishBean.setIp("127.0.0.1");
			publishBean.setMsgUniqueId(msguniqueid);
			publishBean.setBody(body);

			List<MQMessageStatusBean> statusList = new MQMessageStatusWapper().getStatusByMid(appName, msguniqueid);
			int num1 = statusList.size();

			publishCall.setData(publishBean);
			publishCall.callService();

			Thread.sleep(2000);

			Logger.verifyEquals(ECode.SUCCESS, publishCall.getCode(), "验证Code");
			Logger.verifyEquals("NULL", publishCall.getMsg().toUpperCase(), "验证Msg");

			MQMessageBean messageBean = new MQMessageWapper().getMessageByMid(appName, codeName, msguniqueid);

			int flag = 0;

			while (messageBean == null || messageBean.getPstatus() != 2 && flag < 20) {
				Thread.sleep(200);
				flag++;
				messageBean = new MQMessageWapper().getMessageByMid(appName, codeName, msguniqueid);
			}

			Date nowTime = new Date();
			Date date = new Date(nowTime.getTime() - 8 * 60 * 60 * 1000);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH");

			// 验证消息成功
			Logger.verifyEquals(publishBean.getAppId(), messageBean.getAid(), "验证数据库Aid");
			Logger.verifyEquals(publishBean.getCode(), messageBean.getCode(), "验证数据库Code");
			Logger.verifyEquals(publishBean.getIp(), messageBean.getIp(), "验证数据库Ip");
			Logger.verifyEquals("\"" + publishBean.getBody() + "\"", messageBean.getBody(), "验证数据库Body");
			Logger.verifyEquals(sdf.format(date), messageBean.getCtime().toString().subSequence(7, 20), "验证数据库Ctime");
			Logger.verifyEquals(true, messageBean.getBusIp().contains("130"), "验证数据库BusIp");
			Logger.verifyEquals(2, messageBean.getNstatus(), "验证数据库nstatus:2-分发进入补单");
			Logger.verifyEquals(sdf.format(date), messageBean.getPushtime().toString().subSequence(7, 20),
					"验证数据库Pushtime");
			Logger.verifyEquals(2, messageBean.getPstatus(), "验证数据库pstatus:2-补单中");

			// 验证进入补单库
			MQMessageCompensateBean compensate = new MQMessageCompensateWapper().getCompensateByUuid(appName, codeName,
					messageBean.getUuid());

			Logger.verifyEquals(messageBean.getAid(), compensate.getAppid(), "验证数据库MQ_Message_Compensate字段AppId");
			Logger.verifyEquals(messageBean.getCode(), compensate.getCode(), "验证数据库MQ_Message_Compensate字段Code");
			Logger.verifyEquals(messageBean.getMid(), compensate.getMid(), "验证数据库MQ_Message_Compensate字段mId");
			// Logger.verifyEquals(sdf.format(date),
			// compensate.getRtimeout().toString().subSequence(7, 20),
			// "验证数据库MQ_Message_Compensate字段Rtimeout");
			Logger.verifyEquals(true, compensate.getRetrycount() > 0, "验证数据库MQ_Message_Compensate字段RetryCount");
			Logger.verifyEquals(3, compensate.getSource(), "验证数据库MQ_Message_Compensate字段Source补单来源:2-分发站");
			Logger.verifyEquals(1, compensate.getNstatus(),
					"验证数据库MQ_Message_Compensate字段nStatus:0-未补发,1-补发中,2-补发成功,3-补发失败");
			Logger.verifyEquals("kafkaApp_miaosi_c0", compensate.getCid(), "验证数据库MQ_Message_Compensate字段Cid");

			// 验证Status表生成记录
			statusList = new MQMessageStatusWapper().getStatusByMid(appName, msguniqueid);
			int num2 = statusList.size();

			Logger.verifyEquals(true, num2 - num1 >= 1, "验证数据库MQ_Message_Status新生成一条记录");

			MQMessageStatusBean status = statusList.get(num2 - 1);

			SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd'T'HH");

			Logger.verifyEquals(messageBean.getUuid(), status.getUuid(), "验证数据库MQ_Message_Status字段uuid");
			Logger.verifyEquals(messageBean.getMid(), status.getMid(), "验证数据库MQ_Message_Status字段Mid");
			Logger.verifyEquals("kafkaApp_miaosi_c0", status.getCid(), "验证数据库MQ_Message_Status字段Cid");
			Logger.verifyEquals("PushFail", status.getStatus(), "验证数据库MQ_Message_Status字段Source");
			Logger.verifyEquals("SecondCompensate", status.getSource(), "验证数据库MQ_Message_Status字段uuid");
			Logger.verifyEquals(sdf2.format(date), status.getCtime().toString().subSequence(7, 20),
					"验证数据库MQ_Message_Status字段Ctime");
			Logger.verifyEquals(true, status.getResult().contains("fail, kafkaApp_miaosi_c0"),
					"验证数据库MQ_Message_Status字段Result");
			Logger.verifyEquals(true,
					status.getResult().contains("http://mockforpay.iapi.ymatou.com/api/messagebus/return/fail"),
					"验证数据库MQ_Message_Status字段Result");
			Logger.verifyEquals(true, status.getR_ip().contains("135"), "验证数据库MQ_Message_Status字段R_ip");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	@Ignore
	public void Tc_Publish_013() {
		Logger.start(true, "发布消息-200发布成功-接收失败-定时补单");
		try {

			String appName = "kafkaApp";
			String codeName = "dingshisi";

			// String msguniqueid = TestDataManager.getRandomUUID().toString();
			String msguniqueid = TestDataManager.getRandomNum(10);
			String body = TestDataManager.randomStr(20);
			publishBean.setAppId(appName);
			publishBean.setCode(codeName);
			publishBean.setIp("127.0.0.1");
			publishBean.setMsgUniqueId(msguniqueid);
			publishBean.setBody(body);

			List<MQMessageStatusBean> statusList = new MQMessageStatusWapper().getStatusByMid(appName, msguniqueid);
			int num1 = statusList.size();

			publishCall.setData(publishBean);
			publishCall.callService();

			Thread.sleep(1000);

			Logger.verifyEquals(ECode.SUCCESS, publishCall.getCode(), "验证Code");
			Logger.verifyEquals("NULL", publishCall.getMsg().toUpperCase(), "验证Msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_Publish_014() {
		Logger.start(true, "发布消息-400-参数异常");
		try {

			// String msguniqueid = TestDataManager.getRandomUUID().toString();
			String msguniqueid = TestDataManager.getRandomNum(10);
			String body = TestDataManager.randomStr(20);
			String appName = "testbamei";
			String codeName = "qusi";
			// String codeName = "miaosi";

			List<MQMessageStatusBean> statusList = new MQMessageStatusWapper().getStatusByMid(appName, msguniqueid);
			int num1 = statusList.size();

			publishBean.setAppId(appName);
			publishBean.setCode(codeName);
			publishBean.setIp("127.0.0.1");
			publishBean.setMsgUniqueId(msguniqueid);
			publishBean.setBody(body);

			publishCall.setData(publishBean);
			publishCall.callService();

			Thread.sleep(1000);

			Logger.verifyEquals("400", publishCall.getCode(), "验证Code");
			Logger.verifyEquals("参数异常|invalid appId:testbamei, please config mqtype to kafka.", publishCall.getMsg(),
					"验证Msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_Publish_0015() {
		Logger.start(true, "发布消息-200发布成功-接收成功-EnableLog=False的Code不记录MongoDB日志-JIRACONN-2948");
		try {

			// String msguniqueid = TestDataManager.getRandomUUID().toString();
			String msguniqueid = TestDataManager.getRandomNum(10);
			String body = TestDataManager.randomStr(20);
			String appName = "kafkaApp";
			String codeName = "nolog";
			// String codeName = "miaosi";

			List<MQMessageStatusBean> statusList = new MQMessageStatusWapper().getStatusByMid(appName, msguniqueid);
			int num1 = statusList.size();

			publishBean.setAppId(appName);
			publishBean.setCode(codeName);
			publishBean.setIp("127.0.0.1");
			publishBean.setMsgUniqueId(msguniqueid);
			publishBean.setBody(body);

			publishCall.setData(publishBean);
			publishCall.callService();

			Thread.sleep(1000);

			Logger.verifyEquals(ECode.SUCCESS, publishCall.getCode(), "验证Code");
			Logger.verifyEquals("NULL", publishCall.getMsg().toUpperCase(), "验证Msg");

			MQMessageBean messageBean = new MQMessageWapper().getMessageByMid(appName, codeName, msguniqueid);

			Thread.sleep(1000);

			Logger.verifyIsNull(messageBean, "验证未进Message表");

			// 验证Status表生成记录
			statusList = new MQMessageStatusWapper().getStatusByMid(appName, msguniqueid);
			int num2 = statusList.size();

			Logger.verifyEquals(0, num2, "验证数据库MQ_Message_Status新生成一条记录");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_Publish_0016() {
		Logger.start(true, "发布消息-200发布成功-接收失败-EnableLog=False的Code不记录MongoDB日志-JIRACONN-2948");
		try {

			// String msguniqueid = TestDataManager.getRandomUUID().toString();
			String msguniqueid = TestDataManager.getRandomNum(10);
			String body = TestDataManager.randomStr(20);
			String appName = "kafkaApp";
			String codeName = "testlog";
			// String codeName = "miaosi";

			// List<MQMessageStatusBean> statusList = new
			// MQMessageStatusWapper().getStatusByMid(appName, msguniqueid);
			// int num1 = statusList.size();

			Date nowTime = new Date();
			Date date = new Date(nowTime.getTime() - 8 * 60 * 60 * 1000);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH");

			publishBean.setAppId(appName);
			publishBean.setCode(codeName);
			publishBean.setIp("127.0.0.1");
			publishBean.setMsgUniqueId(msguniqueid);
			publishBean.setBody(body);

			publishCall.setData(publishBean);
			publishCall.callService();

			Thread.sleep(1000);

			Logger.verifyEquals(ECode.SUCCESS, publishCall.getCode(), "验证Code");
			Logger.verifyEquals("NULL", publishCall.getMsg().toUpperCase(), "验证Msg");

			MQMessageBean messageBean = new MQMessageWapper().getMessageByMid(appName, codeName, msguniqueid);

			Thread.sleep(1000);

			Logger.verifyIsNull(messageBean, "验证未进Message表");

			// 验证Status表生成记录
			List<MQMessageStatusBean> statusList = new MQMessageStatusWapper().getStatusByMid(appName, msguniqueid);
			int num2 = statusList.size();

			Logger.verifyEquals(1, num2, "验证数据库MQ_Message_Status新生成一条记录");

			MQMessageStatusBean status = statusList.get(num2 - 1);

			SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd'T'HH");

			Logger.verifyEquals(msguniqueid, status.getMid(), "验证数据库MQ_Message_Status字段uuid");
			Logger.verifyEquals("KafkaApp_log_c1", status.getCid(), "验证数据库MQ_Message_Status字段Cid");
			Logger.verifyEquals("PushFail", status.getStatus(), "验证数据库MQ_Message_Status字段Source");
			Logger.verifyEquals("Dispatch", status.getSource(), "验证数据库MQ_Message_Status字段uuid");
			Logger.verifyEquals(sdf2.format(date), status.getCtime().toString().subSequence(7, 20),
					"验证数据库MQ_Message_Status字段Ctime");
			Logger.verifyEquals(true, status.getResult().contains("fail, KafkaApp_log_c1"),
					"验证数据库MQ_Message_Status字段Result");
			Logger.verifyEquals(true,
					status.getResult().contains("http://mockforpay.iapi.ymatou.com/api/messagebus/return/fail"),
					"验证数据库MQ_Message_Status字段Result");

		} catch (Exception e) {
			Logger.fail(e);
		}

	}
		@Test
		@Category({ P1.class })
		@TestCase
		public void Tc_Publish_0017() {
			Logger.start(true, "发布消息-200发布成功-接收失败");
			try {

				String appName = "test5000_kafka";
				String codeName = "test5000";

				int i=0;
				while(i<10)
				{
					publishBean.setAppId(appName);
					publishBean.setCode(codeName);
					publishBean.setIp("127.0.0.1");
					publishBean.setMsgUniqueId(TestDataManager.getRandomNum(10));
					publishBean.setBody(TestDataManager.getRandomNum(20));
					
					publishCall.setData(publishBean);
					publishCall.callService();
					
					Thread.sleep(100);
					i++;
				}

			} catch (Exception e) {
				Logger.fail(e);
			}
		}

		@Test
		@Category({ P1.class })
		@TestCase
		public void Tc_Publish_018() {
			Logger.start(true, "发布消息-200发布成功-去空格");
			try {

				// 只支持sit，uat没陪app跟code
				// String msguniqueid = TestDataManager.getRandomUUID().toString();
				String msguniqueid = TestDataManager.getRandomNum(10);
				String body = TestDataManager.randomStr(20);
				String appName = "qukongge_kafka";
				String codeName = "qukongge";
				// String codeName = "miaosi";

				List<MQMessageStatusBean> statusList = new MQMessageStatusWapper().getStatusByMid(appName, msguniqueid);
				int num1 = statusList.size();

				publishBean.setAppId(appName);
				publishBean.setCode(codeName);
				publishBean.setIp("127.0.0.1");
				publishBean.setMsgUniqueId(msguniqueid);
				publishBean.setBody(body);

				publishCall.setData(publishBean);
				publishCall.callService();
				
				Thread.sleep(1000);

				Logger.verifyEquals(ECode.SUCCESS, publishCall.getCode(), "验证Code");
				Logger.verifyEquals("NULL", publishCall.getMsg().toUpperCase(), "验证Msg");

				MQMessageBean messageBean = new MQMessageWapper().getMessageByMid(appName, codeName, msguniqueid);

				int flag = 0;

				while (messageBean == null || messageBean.getPstatus() != 1 && flag < 20) {
					Thread.sleep(200);
					flag++;
					messageBean = new MQMessageWapper().getMessageByMid(appName, codeName, msguniqueid);
				}

				Date nowTime = new Date();
				Date date = new Date(nowTime.getTime() - 8 * 60 * 60 * 1000);
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH");

				// 验证Message表
				Logger.verifyEquals(publishBean.getAppId(), messageBean.getAid(), "验证数据库Aid");
				Logger.verifyEquals(publishBean.getCode(), messageBean.getCode(), "验证数据库Code");
				Logger.verifyEquals(publishBean.getIp(), messageBean.getIp(), "验证数据库Ip");
				Logger.verifyEquals("\"" + publishBean.getBody() + "\"", messageBean.getBody(), "验证数据库Body");
				Logger.verifyEquals(sdf.format(date), messageBean.getCtime().toString().subSequence(7, 20), "验证数据库Ctime");
				Logger.verifyEquals(true, messageBean.getBusIp().contains("130"), "验证数据库BusIp");

				Logger.verifyEquals(0, messageBean.getNstatus(), "验证数据库nstatus");

				String pushTime = messageBean.getPushtime().toString();

				Logger.verifyEquals(sdf.format(date), pushTime.subSequence(7, 20), "验证数据库Pushtime");

				Logger.verifyEquals(1, messageBean.getPstatus(), "验证数据库Pushstatus:1-分发成功");

				// 验证Status表生成记录
				statusList = new MQMessageStatusWapper().getStatusByMid(appName, msguniqueid);
				int num2 = statusList.size();

				Logger.verifyEquals(1, num2 - num1, "验证数据库MQ_Message_Status新生成一条记录");

				MQMessageStatusBean status = statusList.get(num2 - 1);

				Logger.verifyEquals(messageBean.getUuid(), status.getUuid(), "验证数据库MQ_Message_Status字段uuid");
				Logger.verifyEquals(messageBean.getMid(), status.getMid(), "验证数据库MQ_Message_Status字段Mid");
				Logger.verifyEquals("qukongge_kafka_qukongge_c0", status.getCid(), "验证数据库MQ_Message_Status字段Cid");
				Logger.verifyEquals("PushOk", status.getStatus(), "验证数据库MQ_Message_Status字段Status");
				Logger.verifyEquals("Dispatch", status.getSource(), "验证数据库MQ_Message_Status字段Source");
				Logger.verifyEquals(sdf.format(date), status.getCtime().toString().subSequence(7, 20),
						"验证数据库MQ_Message_Status字段Ctime");
				Logger.verifyEquals(true, status.getResult().contains("ok, qukongge_kafka_qukongge_c0"),
						"验证数据库MQ_Message_Status字段Result");
				Logger.verifyEquals(true,
						status.getResult().contains("http://172.16.103.114:8034/Monitor/returnok"),
						"验证数据库MQ_Message_Status字段Result");
			} catch (Exception e) {
				Logger.fail(e);
			}
		}
		
		@Test
		@Category({ P1.class })
		@TestCase
		public void Tc_Publish_019() {
			Logger.start(true, "发布消息-200发布成功");
			try {

				// 只支持sit，uat没陪app跟code
				// String msguniqueid = TestDataManager.getRandomUUID().toString();
				String msguniqueid = TestDataManager.getRandomNum(10);
				String body = TestDataManager.randomStr(20);
				String appName = "kafkaApp";
				String codeName = "7569";
				// String codeName = "miaosi";

				publishBean.setAppId(appName);
				publishBean.setCode(codeName);
				publishBean.setIp("127.0.0.1");
				publishBean.setMsgUniqueId(msguniqueid);
				publishBean.setBody(body);

				publishCall.setData(publishBean);
				publishCall.callService();
				
			} catch (Exception e) {
				Logger.fail(e);
			}
		}
}